//
//  ViewController.m
//  UI0508__通讯录作业
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "ListModel.h"
#import "SecondViewController.h"
#import "ContactsViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *myTableView;

@property(nonatomic, retain)NSDictionary *dictionary;
//数组有序
@property(nonatomic, retain)NSArray *keyArray;

@property(nonatomic, retain)NSMutableArray *myArray;

@end

@implementation ViewController

- (void)dealloc{
    
    [_myArray release];
    [_dictionary release];
    [_keyArray release];
    [super dealloc];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.dictionary = [[NSDictionary alloc] init];
    self.keyArray = [[NSArray alloc] init];
    self.myArray = [[NSMutableArray alloc] init];
    
    [self handData];
    [self creatTableView];
    
    
    
    self.navigationItem.title = @"联系人";
    
    
    
}


- (void)creatTableView{
    
   self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    // 建造重用池, 注册 reuse
    [self.myTableView registerClass:[ContactsViewCell class] forCellReuseIdentifier:@"reuse"];
    
}

#pragma mark - 取出数据
- (void)handData{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contact" ofType:@".plist"];
    // 从文件中获取
    self.dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    
    self.keyArray = [self.dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
   
   

    
}




#pragma mark -- UITableViewCell 必须实现的两个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    ContactsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
  // 取消UItableview 的点击方法
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 根据section 取出联系人分组
    NSArray *array = [self.dictionary objectForKey:[self.keyArray objectAtIndex:indexPath.section]];
    // 根据row取出对应的联系人.
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
        ListModel *model = [[ListModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        // 将model 保存到属性数组中
        [self.myArray addObject:model];
        cell.listmodel = model;
//     cell.myImageView.image = [UIImage imageNamed:model.header];
//     cell.myLabelNumber.text = model.number;
//     cell.myLabelNames.text = model.name;

     //   NSLog(@"%@",model.name);
    //[self.myTableView reloadData];
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    // 根据 section 取出分区对应的联系人分组的key
    NSString *key = [self.keyArray objectAtIndex:section];
    // 再根据key取出对应的联系人分组
    NSArray *array = [self.dictionary objectForKey:key];
    
    return array.count;
}

// 返回分区头的标题   // 显示分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    //    NSString *headerString = [[self.dictionary allKeys] objectAtIndex:section];
    
    NSString *headerString = [self.keyArray  objectAtIndex:section];
    return headerString;
}



// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dictionary.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
    
    return 150;
}

// 侧边的小东西

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return self.keyArray;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
    
}




/*NSLog(@"section : %ld, row : %ld", indexPath.section, indexPath.row);
*/



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
